package me.gacl.websocket;

import me.gacl.websocket.JSR356.example.SessionUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


/**
 *
 * 轮循任务监听器
 * @author gacl
 *
 */
@WebListener
public class RotationTaskListener implements ServletContextListener {

    /**
     * 轮询任务管理器
     */
    private RotationTaskManager rotationTaskManager;

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("--RotationTaskListener销毁--");
        //停止所有正在轮询的任务
        rotationTaskManager.stopAllTask(true);
        rotationTaskManager.release();
        SessionUtils.releaseAllSession();
    }

    public void contextInitialized(ServletContextEvent sce) {

        System.out.println("ServletContex初始化");
        System.out.println(sce.getServletContext().getServerInfo());
        System.out.println("--RotationTaskListener初始化--");
        rotationTaskManager = RotationTaskManager.getInstance();
        // 添加数据推送轮询任务
        rotationTaskManager.addTaskAndRun(new DataPushTask(),DataPushTask.FIRST_RUN_DELAY_SECONDS, DataPushTask.PERIOD_SECONDS);
    }


}
